<?php
// +----------------------------------------------------------------------
// | OpenCMF [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.opencmf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 廖权名  1528490339@qq.com
// +----------------------------------------------------------------------
namespace Yiban\Admin;

use Admin\Controller\AdminController;
use Common\Util\Think\Page;

/**
 * 新闻控制器
 * @author 廖权名  1528490339@qq.com
 */
class LotteryAdmin extends AdminController
{
    /**
     * 默认方法
     * @author 廖权名  1528490339@qq.com
     */
    public function index()
    {

        $keyword = I('keyword', '', 'string');
        $condition = array('like', '%' . $keyword . '%');
        $map['id|title'] = array(
            $condition,
            $condition,
            $condition,
            $condition,
            $condition,
            '_multi' => true
        );

        // 获取所有项目
        $map['status'] = array('egt', '0'); // 禁用和正常状态
        $p = !empty($_GET["p"]) ? $_GET['p'] : 1;
        $user_object = D('Yiban/Lottery');
        $data_list = $user_object
            ->page($p, C('ADMIN_PAGE_ROWS'))
            ->where($map)
            ->order('id desc')
            ->select();
        $page = new Page(
            $user_object->where($map)->count(),
            C('ADMIN_PAGE_ROWS')
        );
        $setting['name']  = 'setting';
        $setting['title'] = '设置';
        $setting['class'] = 'label label-success';
        $setting['href']  = U('setting', array('id' => '__data_id__'));



        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('抽奖管理')// 设置页面标题
        ->addTopButton('addnew')// 添加新增按钮
        ->addTopButton('resume', array('model' => 'sport_news'))// 添加启用按钮
        ->addTopButton('forbid', array('model' => 'sport_news'))// 添加禁用按钮
        ->addTopButton('delete', array('model' => 'sport_news'))// 添加删除按钮
        ->setSearch('请输入ID/新闻标题', U('index'))
            ->addTableColumn('id', '编号')
            ->addTableColumn('title', '标题')
            ->addTableColumn('content', '内容')
            ->addTableColumn('status', '状态', 'status')
            ->addTableColumn('right_button', '操作', 'btn')
            ->setTableDataList($data_list)// 数据列表
            ->setTableDataPage($page->show())// 数据列表分页
            ->addRightButton('self',$setting)// 添加编辑按钮
            ->addRightButton('edit')// 添加编辑按钮
            ->addRightButton('forbid')// 添加禁用/启用按钮
            ->addRightButton('recycle')// 添加删除按钮
            ->display();
    }

    public function setting($id)
    {
        //搜索
        $keyword = I('keyword', '', 'string');
        $condition = array('like','%'.$keyword.'%');
        $map['id|uname'] = array(
            $condition,
            $condition,
            '_multi'=>true
        );
        $p = !empty($_GET["p"]) ? $_GET['p'] : 1;
        $user_object = D('Market/User');
        $map['pid_1']=$id;
        $data_list = $user_object
            ->page($p, C('ADMIN_PAGE_ROWS'))
            ->where($map)
            ->order('id asc')
            ->select();

        $page = new Page(
            $user_object->where($map)->count(),
            C('ADMIN_PAGE_ROWS')
        );

        // 使用Builder快速建立列表页面。
        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('导航列表') // 设置页面标题
        ->addTopButton('resume')  // 添加启用按钮
        ->addTopButton('forbid')  // 添加禁用按钮
        ->setSearch('请输入ID/用户名', U('index'))
            ->addTableColumn('uname', '用户名')
            ->addTableColumn('pid_1', '推荐人', 'callback', array(D('User'), 'user_name'))
            ->addTableColumn('allmoney', '总盈利')
            ->addTableColumn('id', '今日盈利','callback', array(D('Money'), 'today_money'))
            ->addTableColumn('status', '是否会员','status')
            ->addTableColumn('right_button', '操作', 'btn')
            ->setTableDataList($data_list)  // 数据列表
            ->setTableDataPage($page->show())// 数据列表分页
            ->addRightButton('edit', array('href' => U('edit', array('id' => '__data_id__'))))  // 添加编辑按钮
            ->addRightButton('forbid')      // 添加禁用/启用按钮
            ->display();
    }



    /**
     * 新增新闻
     * @author 廖权名  1528490339@qq.com
     */
    public function add()
    {
        if (IS_POST) {
            $user_object = D('Yiban/Jobs');
            $data = $user_object->create();
            if ($data) {
                $id = $user_object->add();
                if ($id) {
                    $this->success('新增成功', U('index'));
                } else {
                    $this->error('新增失败');
                }
            } else {
                $this->error($user_object->getError());
            }
        } else {
            // 使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('新增')//设置页面标题
            ->setPostUrl(U('add'))//设置表单提交地址
            ->addFormItem('title', 'text', '招聘会标题', '招聘会标题')
                ->addFormItem('url', 'text', '招聘会链接', '招聘会链接')
                ->addFormItem('order','text', '排序','（选填）数字越大显示越前')
                ->display();
        }
    }

    /**
     * 编辑新闻
     * @author 廖权名  1528490339@qq.com
     */
    public function edit($id)
    {
        if (IS_POST) {
            // 提交数据
            $user_object = D('Yiban/Jobs');
            $data = $user_object->create();
            if ($data) {
                $result = $user_object
                    ->field('id,title,url')
                    ->save($data);
                if ($result) {
                    $this->success('更新成功', U('index'));
                } else {
                    $this->error('更新失败', $user_object->getError());
                }
            } else {
                $this->error($user_object->getError());
            }
        } else {
            // 获取账号信息
            $info = D('Yiban/Jobs')->find($id);

            // 使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('编辑新闻')// 设置页面标题
            ->setPostUrl(U('edit'))// 设置表单提交地址
            ->addFormItem('id', 'hidden', 'ID', 'ID')
                ->addFormItem('title', 'text', '招聘会标题', '招聘会标题')
                ->addFormItem('url', 'text', '招聘会链接', '招聘会链接')
                ->addFormItem('order', 'text','排序','（选填）数字越大显示越前')
                ->setFormData($info)
                ->display();
        }
    }


}
